# File containing the definition of a filter
# A filter is defined by four concepts:
#  1) the database where the data is stored
#  2) the type of data to which apply the filter
#  3) the specific field of the data to which the filter will be applied
#  4) the regular expression used to apply the filter

import re

class Filter:
    
    def __init__(self, database, data_type, field, reg_exp):
        self.database = database
        self.data_type = data_type
        self.field = field
        self.reg_exp = reg_exp
        
    def apply_to_data(self, data):
        # apply the defined regular expression to the given data
        # data is intended to be a dictionary
        # return True if match found, False otherwise
        pattern = re.compile(self.reg_exp)
        result = pattern.match(data[self.field])
        if result:
            return True
        else:
            return False      
        
    def equals(self, filter):
        # return true if the current filter is equal to the given filter
        return self.database==filter.database and self.data_type==filter.data_type and self.field==filter.field and self.reg_exp==filter.reg_exp  
    
    def __str__(self):
        return "<"+self.data_type+" - "+self.field+" - "+self.reg_exp+">" 